package IO;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class IODemo5 {
    public static void main(String[] args) throws IOException {
        /*OutputStream outputStream = new FileOutputStream("d:/test.txt");
        // write 会清空原有的文件 再写
        outputStream.write(97);
        outputStream.write(98);
        outputStream.write(99);
        // 需要保证close操作的执行
        outputStream.close();*/

        /*// 这种写法还是不够优雅
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("d:/test.txt");
            outputStream.write(97);
            outputStream.write(98);
            outputStream.write(99);
        }finally {
            outputStream.close();
        }*/

        // 推荐写法
        try (OutputStream outputStream = new FileOutputStream("d:/test.txt")){
            outputStream.write(97);
            outputStream.write(98);
            outputStream.write(99);
        }
        // try执行完 自动执行close
    }
}
